package org.blade.personal.manager.sys.corncre;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.blade.personal.dao.sys.AuthorityDao;
import org.blade.personal.dao.sys.SystemRefAuthorityDao;
import org.blade.personal.manager.sys.AuthorityManager;
import org.blade.personal.mode.sys.SystemAuthority;
import org.blade.personal.mode.sys.SystemRefAuthority;
import org.blade.personal.mode.sys.SystemUser;
import org.blade.personal.utils.Pager;
import org.blade.personal.utils.TreeUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional("transactionManager")
@Service("AuthorityManager")
public class AuthorityManagerImpl implements AuthorityManager{

	@Resource
	private AuthorityDao authorityDao;
	
	@Resource
	private SystemRefAuthorityDao systemRefAuthorityDao;
	
	@Override
	public void saveAuthority(SystemAuthority authority) {
		
		if( null == authority.getId() ){
			authorityDao.addAuthority(authority);
		}else {
			authorityDao.updateAuthority(authority);
		}
		
	}

	@Override
	public int deleteAuthority(String authIds) {
		
		String deleteSql = "delete from sys_authority where id in (?)";
		return authorityDao.deleteAuthority(deleteSql, new Object[]{authIds});
	}

	@Override
	public int deleteAuthorityByUser(Long usrId, String authIds) {
		
		String deleteSql = "delete from sys_authority where id in (?) and user_id = ?";
		return authorityDao.deleteAuthority(deleteSql, new Object[]{authIds,usrId});
	}

	@Override
	public int deleteAuthorityByRole(Long roleId, String authIds) {
		String deleteSql = "delete from sys_authority where id in (?) and role_id = ?";
		return authorityDao.deleteAuthority(deleteSql, new Object[]{authIds,roleId});
	}

	@Override
	public void updateAuthority(SystemAuthority authority) {
		 authorityDao.updateAuthority(authority);
	}

	@Override
	public void batchSave(List<SystemAuthority> authorities) {
		 authorityDao.batchSave(authorities);
	}

	@Override
	public void  batchUpdate(List<SystemAuthority> authorities) {
		 authorityDao.batchUpdate(authorities);
	}
	
	@Override
	public List<Map<String,Object>> queryAllTheAuthorities(Long userId) {
		StringBuilder querySqlBuff = new StringBuilder();
		
		querySqlBuff.append(" select * from sys_authority a join sys_url u on a.url_id = u.id where a.usr_id = ? ")
		.append(" union  all ")
		.append("select * from sys_authority a join sys_url u on a.url_id = u.id where a.role_id in ( ")
		.append( "select r.id from sys_usr  u left join sys_role r on u.role_id = r.id where u.id = ? )");
		return authorityDao.queryForMapList(querySqlBuff.toString(),
				new Object[]{userId,userId});
	}
	
	@Override
	public void pagingQueryAuthoritiesForUsr(Pager<SystemAuthority> page,Long usrId) {

		String querySql = " select * from sys_authority where usr_id = ? ";
		authorityDao.pagingQuery(page, querySql, new Object[]{usrId});
	}
	
	@Override
	public void pagingQueryAuthoritiesForRole(Pager<SystemAuthority> page, Long roleId) {
		
		String querySql = " select * from  sys_authority where role_id = ? ";
		authorityDao.pagingQuery(page, querySql, new Object[]{roleId});

	}
	
	
	@Override
	public List<Map<String,Object>> queryNavigation(long parentId,long authId,long type, SystemUser usr) {

		String countSubNode = " select count(1) as \"sonNodeNum\", su.parent_id as \"pId\"  from sys_url su left join sys_url pu on su.parent_id = pu.id  where su.parent_id is not null and su.type = 2 group by su.parent_id ";
		//		 
		StringBuilder querySqlBuff = new StringBuilder();
		querySqlBuff.append(" select u.name, u.url, a.weight_value, u.id, su.sonNodeNum from sys_authority a join sys_url u on a.url_id = u.id  ");
		querySqlBuff.append(" left join   (").append(countSubNode).append(") su on su.pId = u.id ");
		querySqlBuff.append(" where a.usr_id = ? and u.parent_id = ? and a.weight_value >= 8 and u.type = ? ");
		
		querySqlBuff.append(" union  all ");
		querySqlBuff.append(" select u.name, u.url, a.weight_value, u.id, su.sonNodeNum  from sys_authority a join sys_url u on a.url_id = u.id ");
		querySqlBuff.append(" left join   (").append(countSubNode).append(") su on su.pId = u.id ");
		querySqlBuff.append(" where a.role_id = ?  and u.parent_id = ? and a.weight_value >= 8  and u.type = ? ");
		
		return authorityDao.queryForMapList(querySqlBuff.toString(), new Object[]{usr.getId(),parentId,type,usr.getRole().getId(),parentId,type});
	}
	
	@Override
	public List<Map<String,Object>> findAuthoritiesOfUser(Long userId) {

		StringBuilder querySqlBuff = new StringBuilder();
		querySqlBuff.append(" select a.* from sys_authority  a join sys_usr u on u.id = a.usr_id  where u.id = ? ");
		return authorityDao.queryForMapList(querySqlBuff.toString(), new Object[]{
			userId
		});
	}
	
	
	@Override
	public List findAuthoritiesByRefIdandRefType(Long id, Long type) {
		
		StringBuilder sql = new StringBuilder();
		sql.append("	select a.*,                                                             ");
		sql.append("    case ra.ref_type when 1 then 1 when 2 then 2 when 3 then 3 end as type  ");
		sql.append("    from sys_authority  a left join sys_ref_authority ra                    ");
		sql.append("	on a.id = ra.sys_authority_id                                           ");
		sql.append("	 left join sys_usr u on u.id = a.usr_id  where 1=1                      ");
		sql.append("	and  u.id = ?  and ra.ref_type = ?                                      ");
		sql.append("    union                                                                   ");
		sql.append("    select a1.*, 0 as type                                                  ");
		sql.append("	from sys_authority  a1 where a1.id not in (                             ");
		sql.append("		select a.id                                                         ");
		sql.append("		from sys_authority  a left join sys_ref_authority ra                ");
		sql.append("		on a.id = ra.sys_authority_id                                       ");
		sql.append("		left join sys_usr u on u.id = a.usr_id  where 1=1                   ");
		sql.append("		and  u.id = ?  and ra.ref_type = ?                                  ");
		sql.append("      )                                                                     ");
		
		List<Map<String,Object>> authorities = authorityDao.queryForMapList(sql.toString(), new Object[]{id,type,id,type});
		return TreeUtils.getTree(authorities);
	}
	
	
	@Override
	public void saveSystemRefAuthorities(
			List<SystemRefAuthority> SystemRefAuthorities) {
		for(SystemRefAuthority sra : SystemRefAuthorities){
			systemRefAuthorityDao.add(sra);
		}
	}
	
	@Override
	public void saveSystemRefAuthority(SystemRefAuthority systemRefAuthority) {
		
		systemRefAuthorityDao.add(systemRefAuthority);
	}
	
	@Override
	public int removeSystemRefAuthority(int refType, Long refId,
			Long authorityId) {
		return 	systemRefAuthorityDao.delete(refType, refId.intValue(), authorityId);
	}
	
}
